package com.hgyc.mom.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.util.poi.ExcelUtil;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.wms.entity.WmsPurchaseReceipt;
import com.hgyc.mom.wms.service.WmsPurchaseReceiptService;
import com.hgyc.mom.wms.vo.WmsPurchaseReceiptParam;
import com.hgyc.mom.wms.vo.WmsPurchaseReceiptVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 采购入库单主Controller
 *
 * @author fwj
 * @since 2025-07-08
 */
@Tag(name = "采购入库单主控制器", description = "采购入库单主控制器")
@RestController
@RequestMapping("/wms/purchase/receipt")
public class WmsPurchaseReceiptController {
    @Autowired
    private WmsPurchaseReceiptService wmsPurchaseReceiptService;

    @Operation(summary = "查询采购入库单主列表", description = "查询采购入库单主列表")
    @PostMapping("/pageList")
    public Result<IPage<WmsPurchaseReceipt>> pageList(@Parameter(description = "查询参数") @RequestBody WmsPurchaseReceiptParam wmsPurchaseReceiptParam) {
        Page<WmsPurchaseReceipt> page = new Page<>(wmsPurchaseReceiptParam.getPage(), wmsPurchaseReceiptParam.getPageSize());
        WmsPurchaseReceipt wmsPurchaseReceipt = new WmsPurchaseReceipt();
        BeanUtils.copyProperties(wmsPurchaseReceiptParam, wmsPurchaseReceipt);
        QueryWrapper<WmsPurchaseReceipt> wrapper = QueryWrapperUtil.build(wmsPurchaseReceipt);
        IPage<WmsPurchaseReceipt> list = wmsPurchaseReceiptService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取采购入库单主详细信息", description = "获取采购入库单主详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "采购入库单主ID") @PathVariable("id") Long id) {
        return Result.success(wmsPurchaseReceiptService.getById(id));
    }
    @Operation(summary = "新增采购入库单主", description = "新增采购入库单主")
    @PostMapping
    public Result<?> save(@Parameter(description = "采购入库单主") @RequestBody WmsPurchaseReceiptVO wmsPurchaseReceiptVO) {
        wmsPurchaseReceiptService.saveReceipt(wmsPurchaseReceiptVO);
        return Result.success();
    }

    @Operation(summary = "修改采购入库单主", description = "修改采购入库单主")
    @PutMapping
    public Result<?> update(@Parameter(description = "采购入库单主") @RequestBody WmsPurchaseReceiptVO wmsPurchaseReceiptVO) {
        wmsPurchaseReceiptService.updateReceipt(wmsPurchaseReceiptVO);
        return Result.success();
    }

    @Operation(summary = "删除采购入库单主", description = "删除采购入库单主")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        wmsPurchaseReceiptService.removeReceipt(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsPurchaseReceipt> util = new ExcelUtil<>(WmsPurchaseReceipt.class);
        util.importTemplateExcel(response, "采购入库单主");
    }

    @Operation(summary = "导出采购入库单主列表", description = "导出采购入库单主列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsPurchaseReceiptParam wmsPurchaseReceiptParam) {
        WmsPurchaseReceipt wmsPurchaseReceipt = new WmsPurchaseReceipt();
        BeanUtils.copyProperties(wmsPurchaseReceiptParam, wmsPurchaseReceipt);
        QueryWrapper<WmsPurchaseReceipt> wrapper = QueryWrapperUtil.build(wmsPurchaseReceipt);
        List<WmsPurchaseReceipt> list = wmsPurchaseReceiptService.list(wrapper);
        ExcelUtil<WmsPurchaseReceipt> util = new ExcelUtil<>(WmsPurchaseReceipt.class);
        util.exportExcel(response, list, "采购入库单主数据");
    }

    @Operation(summary = "导入采购入库单主列表", description = "导入采购入库单主列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsPurchaseReceipt> util = new ExcelUtil<>(WmsPurchaseReceipt.class);
        List<WmsPurchaseReceipt> wmsPurchaseReceiptList = util.importExcel(file.getInputStream());
        String message = wmsPurchaseReceiptService.importData(wmsPurchaseReceiptList, updateSupport);
        return Result.success(message);
    }
}
